package main

import (
	"encoding/json"
	"fmt"
	"log"

	_ "github.com/gogf/gf/contrib/drivers/sqlite/v2"
	"github.com/gogf/gf/v2/os/gctx"

	"goldtree/internal/service"
)

// 示例股票数据
var sampleStocks = []*service.CreateStockReq{
	{
		Code:      "000001",
		Name:      "平安银行",
		Market:    "SZ",
		Industry:  "银行",
		Sector:    "金融",
		MarketCap: 320000000000, // 3200亿
		PeRatio:   5.2,
		PbRatio:   0.8,
	},
	{
		Code:      "000002",
		Name:      "万科A",
		Market:    "SZ",
		Industry:  "房地产",
		Sector:    "房地产",
		MarketCap: 280000000000, // 2800亿
		PeRatio:   7.8,
		PbRatio:   1.1,
	},
	{
		Code:      "600000",
		Name:      "浦发银行",
		Market:    "SH",
		Industry:  "银行",
		Sector:    "金融",
		MarketCap: 350000000000, // 3500亿
		PeRatio:   4.9,
		PbRatio:   0.7,
	},
	{
		Code:      "600036",
		Name:      "招商银行",
		Market:    "SH",
		Industry:  "银行",
		Sector:    "金融",
		MarketCap: 850000000000, // 8500亿
		PeRatio:   6.5,
		PbRatio:   1.2,
	},
	{
		Code:      "600519",
		Name:      "贵州茅台",
		Market:    "SH",
		Industry:  "白酒",
		Sector:    "消费",
		MarketCap: 2200000000000, // 2.2万亿
		PeRatio:   25.8,
		PbRatio:   8.9,
	},
	{
		Code:      "000858",
		Name:      "五粮液",
		Market:    "SZ",
		Industry:  "白酒",
		Sector:    "消费",
		MarketCap: 750000000000, // 7500亿
		PeRatio:   18.5,
		PbRatio:   4.2,
	},
	{
		Code:      "300750",
		Name:      "宁德时代",
		Market:    "SZ",
		Industry:  "新能源",
		Sector:    "科技",
		MarketCap: 1100000000000, // 1.1万亿
		PeRatio:   32.1,
		PbRatio:   6.8,
	},
	{
		Code:      "002415",
		Name:      "海康威视",
		Market:    "SZ",
		Industry:  "安防",
		Sector:    "科技",
		MarketCap: 320000000000, // 3200亿
		PeRatio:   15.6,
		PbRatio:   3.2,
	},
	{
		Code:      "688981",
		Name:      "中芯国际",
		Market:    "SH",
		Industry:  "半导体",
		Sector:    "科技",
		MarketCap: 450000000000, // 4500亿
		PeRatio:   28.9,
		PbRatio:   2.1,
	},
	{
		Code:      "600276",
		Name:      "恒瑞医药",
		Market:    "SH",
		Industry:  "医药",
		Sector:    "医疗",
		MarketCap: 380000000000, // 3800亿
		PeRatio:   22.5,
		PbRatio:   4.6,
	},
}

func main() {
	ctx := gctx.GetInitCtx()

	fmt.Println("=== 股票数据导入工具 ===")

	// 使用导入服务
	importReq := &service.StockImportReq{
		Stocks:       sampleStocks,
		SkipExists:   false,
		UpdateExists: true,
	}

	result, err := service.Stock.ImportStocks(ctx, importReq)
	if err != nil {
		log.Fatal("导入失败:", err)
	}

	// 打印导入结果
	fmt.Printf("导入完成！\n")
	fmt.Printf("总数量: %d\n", result.TotalCount)
	fmt.Printf("成功数量: %d\n", result.SuccessCount)
	fmt.Printf("失败数量: %d\n", result.FailedCount)
	fmt.Printf("跳过数量: %d\n", result.SkippedCount)
	fmt.Printf("更新数量: %d\n", result.UpdatedCount)

	if len(result.Errors) > 0 {
		fmt.Println("错误信息:")
		for _, err := range result.Errors {
			fmt.Printf("  - %s\n", err)
		}
	}

	// 获取统计信息
	fmt.Println("\n=== 股票统计信息 ===")
	stats, err := service.Stock.GetStatistics(ctx)
	if err != nil {
		log.Printf("获取统计信息失败: %v", err)
		return
	}

	statsJson, _ := json.MarshalIndent(stats, "", "  ")
	fmt.Println(string(statsJson))

	fmt.Println("\n=== 股票数据导入完成 ===")
}
